Conditions | 3 |
Paths | 2 |
Total Lines | 114 |
Lines | 114 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* global kirkiControlLoader */ |
||
19 | initKirkiControl: function() { |
||
20 | |||
21 | var control = this, |
||
22 | value = control.getValue(), |
||
23 | picker = control.container.find( '.kirki-color-control' ); |
||
24 | |||
25 | // Hide unnecessary controls if the value doesn't have an image. |
||
26 | if ( _.isUndefined( value['background-image'] ) || '' === value['background-image'] ) { |
||
27 | control.container.find( '.background-wrapper > .background-repeat' ).hide(); |
||
28 | control.container.find( '.background-wrapper > .background-position' ).hide(); |
||
29 | control.container.find( '.background-wrapper > .background-size' ).hide(); |
||
30 | control.container.find( '.background-wrapper > .background-attachment' ).hide(); |
||
31 | } |
||
32 | |||
33 | // Color. |
||
34 | picker.wpColorPicker({ |
||
35 | change: function() { |
||
36 | setTimeout( function() { |
||
37 | control.saveValue( 'background-color', picker.val() ); |
||
38 | }, 100 ); |
||
39 | } |
||
40 | }); |
||
41 | |||
42 | // Background-Repeat. |
||
43 | control.container.on( 'change', '.background-repeat select', function() { |
||
44 | control.saveValue( 'background-repeat', jQuery( this ).val() ); |
||
45 | }); |
||
46 | |||
47 | // Background-Size. |
||
48 | control.container.on( 'change click', '.background-size input', function() { |
||
49 | control.saveValue( 'background-size', jQuery( this ).val() ); |
||
50 | }); |
||
51 | |||
52 | // Background-Position. |
||
53 | control.container.on( 'change', '.background-position select', function() { |
||
54 | control.saveValue( 'background-position', jQuery( this ).val() ); |
||
55 | }); |
||
56 | |||
57 | // Background-Attachment. |
||
58 | control.container.on( 'change click', '.background-attachment input', function() { |
||
59 | control.saveValue( 'background-attachment', jQuery( this ).val() ); |
||
60 | }); |
||
61 | |||
62 | // Background-Image. |
||
63 | control.container.on( 'click', '.background-image-upload-button', function( e ) { |
||
64 | var image = wp.media({ multiple: false }).open().on( 'select', function() { |
||
65 | |||
66 | // This will return the selected image from the Media Uploader, the result is an object. |
||
67 | var uploadedImage = image.state().get( 'selection' ).first(), |
||
68 | previewImage = uploadedImage.toJSON().sizes.full.url, |
||
69 | imageUrl, |
||
70 | imageID, |
||
71 | imageWidth, |
||
72 | imageHeight, |
||
73 | preview, |
||
74 | removeButton; |
||
75 | |||
76 | if ( ! _.isUndefined( uploadedImage.toJSON().sizes.medium ) ) { |
||
77 | previewImage = uploadedImage.toJSON().sizes.medium.url; |
||
78 | } else if ( ! _.isUndefined( uploadedImage.toJSON().sizes.thumbnail ) ) { |
||
79 | previewImage = uploadedImage.toJSON().sizes.thumbnail.url; |
||
80 | } |
||
81 | |||
82 | imageUrl = uploadedImage.toJSON().sizes.full.url; |
||
83 | imageID = uploadedImage.toJSON().id; |
||
84 | imageWidth = uploadedImage.toJSON().width; |
||
85 | imageHeight = uploadedImage.toJSON().height; |
||
86 | |||
87 | // Show extra controls if the value has an image. |
||
88 | if ( '' !== imageUrl ) { |
||
89 | control.container.find( '.background-wrapper > .background-repeat, .background-wrapper > .background-position, .background-wrapper > .background-size, .background-wrapper > .background-attachment' ).show(); |
||
90 | } |
||
91 | |||
92 | control.saveValue( 'background-image', imageUrl ); |
||
93 | preview = control.container.find( '.placeholder, .thumbnail' ); |
||
94 | removeButton = control.container.find( '.background-image-upload-remove-button' ); |
||
95 | |||
96 | if ( preview.length ) { |
||
97 | preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + previewImage + '" alt="" />' ); |
||
98 | } |
||
99 | if ( removeButton.length ) { |
||
100 | removeButton.show(); |
||
101 | } |
||
102 | }); |
||
103 | |||
104 | e.preventDefault(); |
||
105 | }); |
||
106 | |||
107 | control.container.on( 'click', '.background-image-upload-remove-button', function( e ) { |
||
108 | |||
109 | var preview, |
||
110 | removeButton; |
||
111 | |||
112 | e.preventDefault(); |
||
113 | |||
114 | control.saveValue( 'background-image', '' ); |
||
115 | |||
116 | preview = control.container.find( '.placeholder, .thumbnail' ); |
||
117 | removeButton = control.container.find( '.background-image-upload-remove-button' ); |
||
118 | |||
119 | // Hide unnecessary controls. |
||
120 | control.container.find( '.background-wrapper > .background-repeat' ).hide(); |
||
121 | control.container.find( '.background-wrapper > .background-position' ).hide(); |
||
122 | control.container.find( '.background-wrapper > .background-size' ).hide(); |
||
123 | control.container.find( '.background-wrapper > .background-attachment' ).hide(); |
||
124 | |||
125 | if ( preview.length ) { |
||
126 | preview.removeClass().addClass( 'placeholder' ).html( 'No file selected' ); |
||
127 | } |
||
128 | if ( removeButton.length ) { |
||
129 | removeButton.hide(); |
||
130 | } |
||
131 | }); |
||
132 | }, |
||
133 | |||
176 |